﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using FE.Model;
using FE.BLL;
using FE.DAL;
namespace FineUIPro.EmptyProjectNet40
{
    public partial class list : PageBase
    {

        FE.BLL.departments deparmentbll = new FE.BLL.departments();
        FE.BLL.users userbll = new FE.BLL.users();
        //  private addressbookEntities db = new addressbookEntities();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                //btnDelete.OnClientClick = Grid1.GetNoSelectionAlertReference("请至少选择一项！");
                //btnDelete.ConfirmText = String.Format("你确定要删除选中的&nbsp;<b><script>{0}</script></b>&nbsp;行数据吗？", Grid1.GetSelectedCountReference());
                var dptlist = deparmentbll.GetAllList();
                ddlDepartment.DataSource = dptlist;// db.departments.Where(p => true).ToList();
                ddlDepartment.DataTextField = "dptName";
                ddlDepartment.DataValueField = "id";
                ddlDepartment.DataBind();

                ddlsearchdepartment.DataSource = dptlist;
                ddlsearchdepartment.DataTextField = "dptName";
                ddlsearchdepartment.DataValueField = "id";
                ddlsearchdepartment.DataBind();
                // 绑定表格
                BindGrid();

                ddlPageSize.SelectedValue = Grid1.PageSize.ToString();
            }
        }


        #region BindGrid

        private void BindGrid()
        {
            //DataTable table = GetSourceData();
            string strWhere = " 1=1 ";
            if (!string.IsNullOrEmpty(ddlsearchdepartment.SelectedValue ))
            {
                strWhere += " and DepartmentId='"+ ddlsearchdepartment.SelectedValue.Trim()+"'";
            }
            if (ddlgender.SelectedValue != "-1")
            {
                strWhere += " and gender="+ddlgender.SelectedValue+"";
            }
            if (txtKeys.Text.Trim() != "")
            {
                strWhere += " and Name like '%"+ txtKeys.Text.Trim() + "%'";
            }
            var list = userbll.GetModelList(strWhere);// db.users.Where(p => true).ToList();
            Grid1.DataSource = list;//table;
            Grid1.DataBind();
        }

        protected string GetDepartmentName(string dpid)
        {
            int id = 0;
            int.TryParse(dpid, out id);
            var department = deparmentbll.GetModel(id);// db.departments.Find(id);
            if (department != null)
            {
                return department.dptName;
            }
            return "--";
        }


        #endregion

        #region Events

        protected void Grid1_PreRowDataBound(object sender, GridPreRowEventArgs e)
        {
            // 如果绑定到 DataTable，那么这里的 DataItem 就是 DataRowView
           // DataRowView row = e.DataItem as DataRowView;
            //int rowId = Convert.ToInt32(row["Id"]);

            //LinkButtonField editField = Grid1.FindColumn("Edit") as LinkButtonField;
            //editField.OnClientClick = String.Format("showEditWindow('{0}');", rowId);
        }


        // 删除数据
        protected void btnDelete_Click(object sender, EventArgs e)
        {
            if (Grid1.SelectedRowID != "")
            {

                DeleteRowByID(Grid1.SelectedRowID);
                BindGrid();
                //ShowNotify("删除数据成功!（表格数据已重新绑定）");
            }
            else
            {
                Alert.Show("请选择要删除的项！");
            }

        }

        protected void Grid1_RowCommand(object sender, GridCommandEventArgs e)
        {
           
        }


        // 保存数据
        protected void btnSave_Click(object sender, EventArgs e)
        {
            //Alert.Show(hfFormID.Text);
            if (hfFormID.Text == "")
            {
           
                var   user = new  FE.Model.users();
                user.id = System.Guid.NewGuid().ToString("N");
                user.usernum = "0";
                user.Name = txtUserName.Text.Trim();
                user.gender = rblFormGender.SelectedValue == "0" ? 0 : 1;
                user.DepartmentId = int.Parse(ddlDepartment.SelectedValue);
                user.TelNum = txtTelNum.Text.Trim();
                user.Callphone = txtCallPhone.Text.Trim();
                user.CreateDate =Convert.ToDateTime( System.DateTime.Now.ToShortDateString());

                userbll.Add(user);

                BindGrid();

                PageContext.RegisterStartupScript(String.Format("F('{0}').selectRow('{1}');", Grid1.ClientID, hfFormID.Text) + Window1.GetHideReference());
            }
            else
            {
                // Alert.Show("编辑");


                var user =userbll.GetModel(hfFormID.Text);

                user.Name = txtUserName.Text.Trim();
                user.gender = rblFormGender.SelectedValue == "0" ? 0 : 1;
                user.DepartmentId = int.Parse(ddlDepartment.SelectedValue);
                user.TelNum = txtTelNum.Text.Trim();
                user.Callphone = txtCallPhone.Text.Trim();


                userbll.Update(user);
                BindGrid();
                PageContext.RegisterStartupScript(String.Format("F('{0}').selectRow('{1}');", Grid1.ClientID, hfFormID.Text) + Window1.GetHideReference());
            }
        }



        #endregion

        #region 


        // 根据行ID来删除行数据
        private void DeleteRowByID(string id)
        {
            //var user = db.users.Find(id);
            //db.users.Remove(user);
            //db.SaveChanges();
            userbll.Delete(id);
        }



        protected void ddlPageSize_SelectedIndexChanged(object sender, EventArgs e)
        {
            Grid1.PageSize = Convert.ToInt32(ddlPageSize.SelectedValue);

        }




        #endregion

        protected void btnEdit_Click(object sender, EventArgs e)
        {
            var rowid = Grid1.SelectedRowID;
            if (string.IsNullOrEmpty(rowid))
            {
                FineUIPro.Alert.Show("请选择要编辑的项！");
                return;
            }
            else
            {
                var user = userbll.GetModel(rowid);
                //var user = db.users.Where(p => p.id == rowid).FirstOrDefault();

                if (user == null)
                {
                    Alert.Show("数据异常！请稍后再试。");
                    return;
                }
                else
                {
                    this.Window1.Hidden = false;
                    this.Window1.Title = "编辑数据";
                    hfFormID.Text = user.id;
                    this.txtUserName.Text = user.Name;
                    this.rblFormGender.SelectedValue = user.gender == 0 ? "0" : "1";
                    this.ddlDepartment.SelectedValue = user.DepartmentId.ToString();
                    this.txtTelNum.Text = user.TelNum;
                    this.txtCallPhone.Text = user.Callphone;
                }
            }
  
            
        }

        protected void Grid1_Sort(object sender, GridSortEventArgs e)
        {
            BindGrid();
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            BindGrid();
        }
    }
}